package com.smyh.UIM.host.state;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.Toast;

import com.smyh.UIM.CustomDialog;
import com.smyh.UIM.HostFragment;
import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.bean.HostState;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.common.MinaClientThread;
import com.smyh.model.host.state.HostModel;
import com.smyh.protcol.host.HostCMDUtil;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 姜龙 on 2016/7/7.
 */
public class HostStateActivity extends Fragment  {
    ScrollView ktableView;
    Button read_state_btn,checkTimeBtn;
    View view;

   public static Map<String,HostState> infoMap = new HashMap<String,HostState>();//

    Dialog dialog;
    /**
     * 主机状态数据是否刷新，如果读取后设置没有刷新
     */
    static boolean isRefresh = false;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        if(view == null){
            view = inflater.inflate(R.layout.activity_host_state_host, container, false);
        }
        initView();
        initEvent();
        return view;
    }

    private void initEvent() {
        read_state_btn.setOnClickListener(btnClickListen);
        checkTimeBtn.setOnClickListener(btnClickListen);

    }
    Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(MainActivity.context, "主机参数读取失败...", Toast.LENGTH_SHORT).show();
            isRefresh = false;
        }
    };

    static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            HostModel.setTableLayout();
            Toast.makeText(MainActivity.context, "主机参数读取成功...", Toast.LENGTH_SHORT).show();
            isRefresh = true;
        }
    };
    public static void refreshHostState(HostState model){
        if(model != null){
            infoMap.put(MainCahce.chooseDeviceAddr,model);
        }
        handler.sendMessage(new Message());
    }

    private void initView() {
        ktableView = (ScrollView) view.findViewById(R.id.ktable);
        read_state_btn = (Button) view.findViewById(R.id.read_state);
        checkTimeBtn = (Button) view.findViewById(R.id.checkTimeBtn);

        if(ktableView.getChildCount()== 0) {
            ScrollView parent = (ScrollView) HostModel.getTableLayout(getContext()).getParent();
            if (parent != null&&parent.getChildCount()>0) {
                parent.removeAllViews();
            }
            ktableView.addView(HostModel.getTableLayout(getContext()));
        }
    }

    View.OnClickListener btnClickListen = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(MinaClientThread.session != null){
                if(!HostFragment.connection_btn.isChecked()){
                    Toast.makeText(getContext(), "请建立连接", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!HostFragment.ready_btn.isChecked()){
                    Toast.makeText(getContext(), "请ready", Toast.LENGTH_SHORT).show();
                    return;
                }
                switch (v.getId()){
                    case R.id.read_state:{

                        isRefresh = false;
                        dialog = CustomDialog.createLoadingDialog(getContext(),"正在读取数据，请稍后...");
                        MinaClientThread.session.write(HostCMDUtil.callHostMockDataCMD());
                        final Long time = System.currentTimeMillis();
                        dialog.show();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                while (true){
                                    if(System.currentTimeMillis() - time >5000||isRefresh){
                                        dialog.dismiss();
                                        if(!isRefresh) {
                                            handler1.sendMessage(new Message());
                                        }
                                        return;
                                    }
                                    try {
                                        Thread.sleep(300);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }).start();
                        break;
                    }

                    case R.id.checkTimeBtn:{

                        IoBuffer buffer = HostCMDUtil.synchTime();
                        MinaClientThread.session.write(buffer);
                        Toast.makeText(getContext(),"已经使用系统对时...",Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }else{
                Toast.makeText(getContext(), "没有连接WIFI", Toast.LENGTH_SHORT).show();
            }
        }
    };


}
